import { Component, EventEmitter, Injector, Output, ViewChild } from '@angular/core';
import { CreateTaxonomyInput,  NameValue, PagedTaxonomyOut, TaxonomyServiceProxy, UpdateTaxonomyInput } from '@shared/service-proxies/service-proxies';
import { ModalDirective } from 'ngx-bootstrap/modal';
import { finalize } from 'rxjs/operators';
import { AppModalComponentBase } from '@shared/common/app-modal-component-base';

@Component({
    selector: 'create-or-edit-taxonomy-modal',
    templateUrl: './create-or-edit-taxonomy-modal.component.html'
})
export class CreateOrEditTaxonomyModalComponent extends AppModalComponentBase {
    @ViewChild('modal', { static: true }) modal: ModalDirective;
    @Output() callback: EventEmitter<any> = new EventEmitter<any>();

    saving = false;
    loading = false;

    title: string = '';

    selectedValue: string ='';
    options: NameValue[] = [];
    idList: string[] = [];

    data:PagedTaxonomyOut;

    detailPages:NameValue[] = [];
    backendPageList:NameValue[] = [];

    constructor(
        injector: Injector,
        private _taxonomyService: TaxonomyServiceProxy
    ) {
        super(injector);
        this.data = new PagedTaxonomyOut();
    }


    show(data: PagedTaxonomyOut) {
        if(data){
            let item = new PagedTaxonomyOut(); 
            Object.assign(item,data);
            this.data = item;
        }else{
            this.data = new PagedTaxonomyOut();
            this.getOrder(); 
        }

        
        this.modal.show();
    }

    getOrder = function () {
        this.loading = true;
        this._taxonomyService.getMaxTaxonomyOrderInfo()
        .pipe(finalize(() => this.loading = false))
        .subscribe(result=> {
            this.data.norder = result.norder;
        });
    };

    close(): void {
        this.modal.hide();
    }

    save() {
        if (this.data.id) {
            this.saving = true;
            let input : UpdateTaxonomyInput = new UpdateTaxonomyInput({
                title: this.data.title,
                localDisplayName:this.data.localDisplayName,
                norder: this.data.norder,
                isTree: this.data.isTree,
                isActive: this.data.isActive,
                id: this.data.id,
            });
            this._taxonomyService.updateTaxonomy(input)
            .pipe(finalize(() => this.saving = false))
            .subscribe(result=> {
                this.notify.success(this.l('SavedSuccessfully'));
                this.callback.emit();
                this.close();
            });
        }else{
            this.saving = true;
            let input : CreateTaxonomyInput= new CreateTaxonomyInput({
                title: this.data.title,
                localDisplayName:this.data.localDisplayName,
                norder: this.data.norder,
                isTree: this.data.isTree,
                isActive: this.data.isActive,
            });
           
            this._taxonomyService.createTaxonomy(input)
            .pipe(finalize(() => this.saving = false))
            .subscribe(result=> {
                this.notify.success(this.l('SavedSuccessfully'));
                this.callback.emit();
                this.close();
            });
        }
    }
}
